'use strict';

const Controller = require('egg').Controller;
const lodash = require('lodash');

class SysRoleController extends Controller {
    /**
     * 获取列表
     */
    async querylist() {
        const { ctx } = this;
        const { queryParams, pageSize, page } = ctx.request.body;
        let data = await ctx.service.sysRole.querylist(queryParams, pageSize, page);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: { tabledata: data }
        };
    }

    /**
     * 新增
     */
    async add() {
        const { ctx, app } = this;
        console.log("提交的数据", ctx.request.body);
        let submitBody = ctx.request.body;
        //验证必填项
        await app.utils.validator.required(['role_name', 'role_key', 'data_scope'], submitBody);
        //过滤非必要项
        let submitData = await app.utils.validator.getSubmitData(['role_name', 'role_key', 'data_scope', 'remark'], submitBody);

        //添加组件值
        let role_id_uuid = await app.utils.uuid.v1();
        submitData.role_id = role_id_uuid;
        //默认状态正常
        submitData.status = "1";
        //操作相关信息
        let userid = await ctx.service.auth.getColonyUserId();
        submitData.create_by = userid;
        submitData.create_time = new Date();
        submitData.update_by = userid;
        submitData.update_time = new Date();
        //保存
        let rs = await ctx.service.sysRole.create(submitData);
        ctx.body = {
            code: 200,
            msg: "保存成功",
            data: {},
        };
    }

    /**
     * 编辑
     */
    async edit() {
        const { ctx, app } = this;
        console.log("提交的数据", ctx.request.body);
        let submitBody = ctx.request.body;
        //验证主键是否存在
        await app.utils.validator.required(['role_id'], submitBody);
        //过滤非必要项
        let submitData = await app.utils.validator.getSubmitData(['role_name', 'role_key', 'data_scope'], submitBody);
        //操作相关信息
        let userid = await ctx.service.auth.getColonyUserId();
        submitData.update_by = userid;
        submitData.update_time = new Date();
        //保存
        let rs = await ctx.service.sysRole.update(submitData, submitBody.role_id);
        ctx.body = {
            code: 200,
            msg: "编辑成功",
            data: {},
        };
    }

    /**
     * 删除
     */
    async delete() {
        const { ctx, app } = this;
        console.log("提交的数据", ctx.request.body);
        let submitBody = ctx.request.body;
        //验证主键是否存在
        await app.utils.validator.required(['role_id'], submitBody);
        //过滤非必要项
        let submitData = await app.utils.validator.getSubmitData(['role_id'], submitBody);

        //操作相关信息
        //let userid = await ctx.service.auth.getColonyUserId();
        //submitData.update_by = userid;
        //submitData.update_time = new Date();
        //删除
        let rs = await ctx.service.sysRole.deleteLogic(submitData.role_id);
        ctx.body = {
            code: 200,
            msg: "删除成功",
            data: {},
        };
    }

    /**
     * 通过主键查询
     */
    async querybyid() {
        const { ctx, app } = this;
        console.log("提交的数据", ctx.request.body);
        let { role_id } = ctx.request.body;

        //查询
        let rs = await ctx.service.sysRole.queryById(role_id);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: rs,
        };
    }

    /**
     * 根据角色id，查询菜单
     */
    async queryMenuByRoleId() {
        const { ctx, app } = this;
        console.log("提交的数据", ctx.request.body);
        let { role_id } = ctx.request.body;

        //查询
        let rs = await ctx.service.sysRole.queryMenuByRoleId(role_id);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: rs,
        };
    }

    /**
     * 设置角色的菜单权限
     */
    async setRoleMenu() {
        const { ctx, app } = this;
        console.log("提交的数据", ctx.request.body);
        let { role_id,menus } = ctx.request.body;

        //查询
        let rs = await ctx.service.sysRole.setRoleMenu(role_id,menus);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: rs,
        };
    }

    /**
     * 根据角色id，查询功能
     */
    async queryFunctionByRoleId() {
        const { ctx, app } = this;
        console.log("提交的数据", ctx.request.body);
        let { role_id } = ctx.request.body;

        //查询
        let rs = await ctx.service.sysRole.queryFunctionByRoleId(role_id);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: rs,
        };
    }

    /**
     * 设置角色的功能数据权限
     */
    async setRoleFunction() {
        const { ctx, app } = this;
        console.log("提交的数据", ctx.request.body);
        let { role_id,functions } = ctx.request.body;

        //查询
        let rs = await ctx.service.sysRole.setRoleFunction(role_id,functions);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: rs,
        };
    }

}

module.exports = SysRoleController;